package westh.ilib.struts.action.manage.question;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.mule.extras.client.MuleClient;
import org.mule.umo.UMOMessage;

import westh.util.YyAction;
import westh.util.YyDate;
import westh.util.YyID;

/**
 * @author zhangjian
 */
public class QuestionAddAction extends YyAction {
	// define commons log
	private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
			.getLog(QuestionAddAction.class);

	public ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// check log and authority,don`s delete
		westh.log.YyLog.addFootprint(request, "动作－添加问题");
		
		//receive data from jsp
		DynaActionForm f = (DynaActionForm) form;
		String questionTitle = f.getString("questionTitle");
		String questionDetail = f.getString("questionDetail");
		String questioner = f.getString("questioner");
		String questionTime = f.getString("questionTime");
		String questionClose=f.getString("questionClose");
		String bestAnswerIndex = f.getString("bestAnswerIndex");
		
		//get number
		String strId = YyID.getUUID32();
  	/*************************cdoi info start******************************/
	  String rId = "012f717f8dde4129a95837bf153de5a0"+strId;
	  //put data into json 
	  JSONObject jsonCdoi = new JSONObject(); 
	  jsonCdoi.put("WESTDO_HANDLE",rId);                                    //only id
	  jsonCdoi.put("CDOI_AGENCY_ID", "012f717f8dde4129a95837bf153de5a0");   //agency id
	  jsonCdoi.put("DC_LANGUAGE", "c7d2ec860cee4b03ba845da0233ed977");      //language id
	  jsonCdoi.put("CDOI_CUSTOMER_ID", "f41825a60cf44e83b83cbd9114220f29"); //customer id
	  jsonCdoi.put("DC_TYPE", "27278cfe6e1c429aa2197b3ff34a11ab");          //type id
	  jsonCdoi.put("DC_TITLE", questionTitle);                              //title
	  jsonCdoi.put("DC_CREATOR", questioner);                               //creator
	  jsonCdoi.put("DC_SUBJECT", "");                                       //subject
	  jsonCdoi.put("DC_DESCRIPTION", questionDetail);                       //description
	  jsonCdoi.put("DC_PUBLISHER", "");                                     //publisher
	  jsonCdoi.put("DC_CONTRIBUTOR", "");                                   //contributor
	  jsonCdoi.put("DC_DATE", "");                                          //date
	  jsonCdoi.put("DC_FORMAT", "");                                        //format
	  jsonCdoi.put("DC_IDENTIFIER", "");                                    //identifier
	  jsonCdoi.put("DC_SOURCE", "");                                        //source
	  jsonCdoi.put("DC_RELATION", "");                                      //relation
	  jsonCdoi.put("DC_COVERAGE", "");                                      //coverage                         
	  jsonCdoi.put("DC_RIGHTS", "");                                        //right
	  jsonCdoi.put("CDOI_URL_SOURCE", "");                                  //resource url
	  jsonCdoi.put("CDOI_STATUS", "1");                                     //resource regedit status
	  jsonCdoi.put("CDOI_NOTE", "");                                        //note
	  
	  String cdoiJson = jsonCdoi.toString();
	  String cdoiId="";
	  try{
		  MuleClient clientW = new MuleClient();
//		  UMOMessage umo = clientW.send("axis:http://192.168.28.112:4281/Cdoi/cdoiCdoi?method=addJson",cdoiJson,null);
		  UMOMessage umo = clientW.send("axis:http://localhost:4281/Cdoi/cdoiCdoi?method=addJson",cdoiJson,null);
		  cdoiId = (String)umo.getPayload();
	  } catch (Exception e) {
		throw new RuntimeException(e);
	  }
		/** ****************************cdoi info end************************ */

		/** ****************************cdo info start************************ */
		JSONObject jsonObj = new JSONObject();

		jsonObj.put("questionId", cdoiId);                        // question id
		jsonObj.put("questionTitle", questionTitle);         // question title
		jsonObj.put("questionDetail", questionDetail);       // question detail
		jsonObj.put("questioner", questioner);               // questioner
		jsonObj.put("questionTime", questionTime);           // question time
		jsonObj.put("questionClose", questionClose);         // question close time
		jsonObj.put("bestAnswerIndex", bestAnswerIndex);     // best answer id
		String input = jsonObj.toString();

		String id = "";
		try {
			MuleClient clientD = new MuleClient();
			UMOMessage umo = clientD.send("axis:http://localhost:4381/LIBJson/iDlibJson?method=saveQuestion",input, null);
			id = (String) umo.getPayload();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		/** ****************************cdo info end************************ */
		if (id.equals("1")) {
			String insertSubjectIndustry = "问题信息添加成功<br/>";
			request.setAttribute("info", insertSubjectIndustry);
			request.setAttribute("redirect", "../questionAddPre.do");
			return mapping.findForward("info");
		} else {
			String insertSubjectIndustry = "问题信息信息添加失败<br/>";
			request.setAttribute("info", insertSubjectIndustry);
			request.setAttribute("redirect", "../questionAddPre.do");
			return mapping.findForward("info");
		}
	}
}